package org.stwy.price;

import java.util.List;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.rap.common.annotation.MetaData;
import com.rap.common.exception.ServiceException;
import com.rap.frame.web.BaseController;

/*
 * 指导价 控制类
 */
/**
*版权信息 (c) RAP 保留所有权利.
*@author Rap.Codegen
*@Team RAP Team
*@Date 2015-4-4 12:26:43
*<p></p>
*
*@参与人：Rap.Codegen
*/

@Controller
@RequestMapping(value = "/price")
public class PriceController extends BaseController<PriceEntity> {
	
	public PriceController() {
		setResourceIdentity("stwy:price");
	}
	private PriceService getService(){
		return (PriceService)super.baseService;
	}

	@RequestMapping(value = "/gov",method = RequestMethod.GET)
	public String viewGov(Model model) {
		super.assertHasPermission("gov");
		model.addAttribute("priceType", "1");
		return "stwy/price/price_main";
	}
	@RequestMapping(value = "/market",method = RequestMethod.GET)
	public String viewMarket(Model model) {
		super.assertHasPermission("market");
		model.addAttribute("priceType", "2");
		return "stwy/price/price_main";
	}

	@RequestMapping(value = "/select")
	public String select(Model model, String callback, String priceType) {
		model.addAttribute("priceType", priceType);
		model.addAttribute("callback", callback);
		return "stwy/price/price_select";
	}
	
	@Override
	protected void doExport(ServletRequest request, Model model,
			HttpServletResponse response, int maxRow) {
		try {
			List<PriceEntity> list = getDataList(request, maxRow);
			/*******************************组装导出信息 begin****************************************/
			//通过entity中注解的类名作为导出标题，如果没有类名注解则默认标题为：导出
			String type = request.getParameter("search_EQ_priceType");
			String defaultTitle = "指导价";
			if(PriceEntity.PRICE_TYPE_GOV.equals(type)){
				defaultTitle = "政府"+defaultTitle;
			}else if(PriceEntity.PRICE_TYPE_MARKET.equals(type)){
				defaultTitle = "市场"+defaultTitle;
			}
			String fileName = getFileName(defaultTitle);
			String sheetName = getSheetName(defaultTitle);
			String headName = getHeadName(defaultTitle);
			//可以手动指定导出的列 
			//String[] column = {"loginName","userName"};
			//默认导出所有的列和数据
			String[] column = getColumns();
			String[] columnName = getColumnNames(column,PriceEntity.class);
			List<Object[]> rowList = getRowList(list,column);
			/*******************************组装导出信息 end******************************************/
			//导出
			doExport(rowList,fileName,sheetName,headName,columnName,response,(HttpServletRequest)request);
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
